<?php

namespace catchAdmin\shopping\controller;

use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\ShoppingCategory as CategoryModel;

class ShoppingCategory extends CatchController
{
    protected $category;

    public function __construct(CategoryModel $category)
    {
        $this->category = $category;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @return \think\response\Json
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate($this->category->getList());
    }

    /**
     * 添加
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        $req = $request->post();
        $req['deleted_at'] = 0;
        $req['sort'] = (int)$req['sort'];
        return CatchResponse::success($this->category->addData($req));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        return CatchResponse::success($this->category->saveData($id, $req));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        return CatchResponse::success($this->category->destroy($id));
    }

    /**
     * 获取品类
     * @return \think\response\Json
     */
    public function getShoppingCategory(): \think\response\Json
    {
        $list = $this->category->field('id,name')->order('sort', 'desc')->select();
        return CatchResponse::success($list);
    }
}